﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;

namespace ExtendedMvc.Elements {
	public class GridRouteCommand<TModel> : GridCommandBase<GridRouteCommand<TModel>, TModel>
		where TModel : class {
		private string _routeName;
		private string _protocol;
		private string _hostName;
		private string _fragment;
		private RouteValueDictionary _routeValues;

		public GridRouteCommand(IModelViewHelper<IEnumerable<TModel>> helper, string text, string routeName, string protocol = null, string hostName = null, string fragment = null)
			: base(helper, text) {
			_routeName = routeName;
			_protocol = protocol;
			_hostName = hostName;
			_fragment = fragment;
		}

		public GridRouteCommand<TModel> RouteValues(object routeValues) {
			_routeValues = routeValues is RouteValueDictionary ? routeValues as RouteValueDictionary : new RouteValueDictionary(routeValues);
			return this;
		}

		public override string GetLink() {
			return UrlHelper.GenerateUrl(_routeName, null, null, _protocol, _hostName, _fragment, _routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, true);
		}
	}
}
